<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <style>
        * {
            list-style: none;
            margin: 0;
            padding: 0;
        }

        #shop-car li {
            border: 1px dotted red;
            margin: 20px 0;
            padding: 20px;
        }

        .container {
            width: 400px;
            padding: 20px;
        }
    </style>

    <div class="container">
        <input type="checkbox" id="isAllCheck" /> 全选

        <ul id="shop-car">
            <!-- <li>
                <input type="checkbox" />
                <div>商品名称:</div>
                <img src="https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg" alt="">
                <div>商品价格:50</div>
                <div>购买数量:30</div>
                <div>商品规格: 黑</div>
                <div>是否选中：选中吧OR不选中</div>
            </li> -->
        </ul>
        <p id="totalnum">总数量：xxx</p>
        <p id="totalprice">总价格：xxx</p>
    </div>
    <script>
        let products = [
            {
                goods_name: '小米10-1',
                goods_img: 'https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg',
                goods_price: '500',
                goods_attr: '砂石黑4GB+64GB',
                goods_num: 10,
                goods_is_checked: true
            },
            {
                goods_name: '小米10-2',
                goods_img: 'https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg',
                goods_price: '510',
                goods_attr: '砂石黑4GB+64GB',
                goods_num: 50,
                goods_is_checked: true
            }
        ];

        // 1.绑定数据
        // 数据拼接
        let carList = '';
        products.forEach(function(curData){
            carList += `<li>
                <input type="checkbox" ${curData.goods_is_checked?'checked':''} />
                <div>商品名称:${curData.goods_name}</div>
                <img src="${curData.goods_img}" alt="">
                <div>商品价格:${curData.goods_price}</div>
                <div>购买数量:${curData.goods_num}</div>
                <div>商品规格: ${curData.goods_attr}</div>
                <div>是否选中：${curData.goods_is_checked?'选中':'不选中'}</div>
            </li> 
        `;
        });
     

        // 赋值操作
        let shopCar = document.getElementById("shop-car");
        shopCar.innerHTML = carList;


        // 2.全选或不全或
        let isAllChecked = products.every(function(val){
            // 条件
            // return val.goods_is_checked == true;
            return val.goods_is_checked;
        });

        console.log(isAllChecked);

        let isAllCheck = document.getElementById("isAllCheck");
        if(isAllChecked){ //成立
            isAllCheck.checked  =true;
        }


        // 3.计算总的  数量 和 价格

        let totalnum = document.getElementById("totalnum");
        let totalprice = document.getElementById("totalprice");

        let totalNum = 0;
        let totalPrice = 0;

        products.forEach(function(val){
            // 判断
            if(val.goods_is_checked){
                totalNum += val.goods_num;
                // 总价格 = 单价 * 数量
                totalPrice += val.goods_price * val.goods_num;
            }
        });

        console.log(totalNum,totalPrice);
        // 赋值
        totalnum.innerHTML = "总数量："+totalNum;
        totalprice.innerHTML = "总价格："+totalPrice;



        // 变量   常量
        // 数据类型 和 数据结构
        // 字符串扩展   Symbol  数组  对象的扩展  Set   Map
        // ...

    </script>
</body>

</html>